import {ADD_COUNTER,ADD_TO_CART} from "./mutations_type";

export default {
  addCart(context,payload){
    return new Promise(resolve => {
      //1.查找之前数组中是否有该产品
      let oldproduct = context.state.cartlist.find(function (item) {
          return item.iid === payload.iid
        }
      )
      //2.判断是否需要push
      if(oldproduct){
        context.commit(ADD_COUNTER,oldproduct)
        resolve('商品数+1')
      }
      else{
        payload.count = 1
        context.commit(ADD_TO_CART,payload)
        resolve('添加成功！')
      }
    })
  }
}

